/**
 * 
 */
package xx.teach.entity;

import java.sql.SQLException;
import java.util.List;

import javax.sql.RowSet;

import xx.db.QueryRunner;
import xx.db.annotation.Column;
import xx.db.annotation.TableName;
import xx.db.sql.Owner;
import xx.db.sql.Table;

/**
 * @author Administrator 2014-3-18
 */
@TableName(name = "t_group")
public class Group extends Table
{

    /**
     * @param qr
     * @param owner
     */
    public Group(QueryRunner qr, Owner owner)
    {
        super(qr, owner);
    }

    public static final String GROUP_ID = "group_id";

    @Column(name = GROUP_ID, pk = true)
    private long groupId;

    public static final String GROUPNAME = "groupname";

    @Column(name = GROUPNAME)
    private String groupname;

    public long getGroupId()
    {
        return groupId;
    }

    public Group setGroupId(long groupId)
    {
        this.groupId = groupId;
        return this;
    }

    public String getGroupname()
    {
        return groupname;
    }

    public Group setGroupname(String groupname)
    {
        this.groupname = groupname;
        return this;
    }

    /*
     * (non-Javadoc)
     * @see xx.db.sql.Table#delete()
     */
    @Override
    public int delete() throws SQLException
    {
        // 删除相关menu
        delete.from(Meno.class).where(Meno.GROUP_ID + "=" + groupId).go();

        // 删除相关chat
        delete.from(Chat.class).where(Chat.GROUP_ID + "=" + groupId).go();

        // 删除相关的job
        RowSet rs = select.from(Job.class, Job.JOB_ID).where(Job.GROUP_ID + "=" + groupId).query();
        while (rs.next())
        {
            Job job = new Job(qr, owner);
            job.setJobId(rs.getLong(Job.JOB_ID)).delete();
        }

        return super.delete();
    }

    /**
     * 根据groupId分页获取account列表
     * 
     * @param pageSize
     * @param pageNo
     * @param condition
     * @param orderField
     * @return
     * @throws SQLException
     */
    public List<Account> getAccountByGroupId(int pageSize, int pageNo, String condition, String orderField) throws SQLException
    {
        String cnd = Account.GROUP_ID + "=" + groupId;
        if (condition != null && !condition.isEmpty())
            cnd = cnd + condition;

        if (orderField == null || orderField.isEmpty())
            orderField = Account.ACCOUNT_ID;

        return select.from(Account.class).where(condition).orderBy(orderField).limit(pageSize).offset(pageSize * (pageNo - 1)).go();
    }

    /**
     * 获取所有班级列表
     * 
     * @return
     * @throws SQLException
     */
    public List<Group> getAllGroup() throws SQLException
    {
        return select.from(Group.class).asc(Group.GROUPNAME).go();
    }

    /**
     * 根据groupId分页获取meno列表
     * 
     * @return
     * @throws SQLException
     */
    public List<Meno> getMenoByGroupId(int pageSize, int pageNo, String condition, String orderField) throws SQLException
    {
        String cnd = Meno.GROUP_ID + "=" + groupId;
        if (condition != null && !condition.isEmpty())
            cnd = cnd + condition;

        if (orderField == null || orderField.isEmpty())
            orderField = Meno.CREATE_DATE + " desc ";

        return select.from(Meno.class).where(cnd).orderBy(orderField).limit(pageSize).offset(pageSize * (pageNo - 1)).go();
    }

    /**
     * 根据groupId分页获取schedule列表
     * 
     * @return
     * @throws SQLException
     */
    public List<Schedule> getScheduleByGroupId(int pageSize, int pageNo, String condition, String orderField) throws SQLException
    {
        String cnd = Schedule.GROUP_ID + "=" + groupId;
        if (condition != null && !condition.isEmpty())
            cnd = cnd + condition;

        if (orderField == null || orderField.isEmpty())
            orderField = Schedule.CLASS_TIME;

        return select.from(Schedule.class).where(condition).orderBy(orderField).limit(pageSize).offset(pageSize * (pageNo - 1)).go();
    }

    /**
     * 根据groupId分页获取chat列表
     * 
     * @return
     * @throws SQLException
     */
    public RowSet getChatByGroupId(int pageSize, int pageNo, String condition, String orderField) throws SQLException
    {
        String cnd = Chat.GROUP_ID + "=" + groupId;
        if (condition != null && !condition.isEmpty())
            cnd = cnd + condition;

        if (orderField == null || orderField.isEmpty())
            orderField = Chat.SUBMIT_TIME + " desc ";

        return select.from(Chat.class).leftJoin(Account.class, Account.NAME).using(Account.ACCOUNT_ID).where(cnd).orderBy(orderField).limit(pageSize)
                .offset(pageSize * (pageNo - 1)).query();
    }
}
